#include <iostream>
#include "account.h"

Account::Account(void){}

Account::Account(const char* name,unsigned int id,struct tm& regTime,vector<Record>& record,unsigned int credit,
	const char* passwd,unsigned int money,unsigned int deposit,vector<unsigned int>& m_vecList,
		vector<unsigned int>& m_collection) : m_name(name),m_id(id),m_regTime(regTime),m_record(record),m_credit(credit),
			m_passwd(passwd),m_money(money),m_deposit(deposit),m_vecList(m_vecList),m_collection(m_collection){}

//检查密码是否正确
bool Account::check_passwd(const char* passwd)
{   
    return 0 == strcmp(passwd,m_passwd.c_str());
}

//输入书号,约定天数借书，并生成一条借书记录存在容器中
bool Account::borrowBook(unsigned int book_id,unsigned int day) 
{
   BookServiceData bookdata;
   Book* book = bookdata.load(book_id);
   time_t timep;
   struct tm *p;
   time(&timep);
   p=localtime(&timep);
   struct tm tt = {};
   Record record(book->m_record.size()+1,book->m_name.c_str(),book->m_id,this->m_name.c_str(),this ->m_id,*p,day,false,tt);
   
   this->m_record.push_back(record);
   AccountData m;
   m.save(*this);
   delete book;
   return true;
}

//输入书号还书加减信用值，并将之前的借书记录完善
int Account::returnBook(unsigned int book_id)
{
    vector<Record>::iterator it;
    for(it = m_record.begin(); it != m_record.end();it++)
    {
        if(it->m_bookId == book_id && false == it->m_isReturn)
        {
            it->m_isReturn = true;
            time_t timep;
            struct tm *p;
            time(&timep);
            p=localtime(&timep);
            it->m_retTime = *p;
            unsigned int old = mktime(&it->m_broTime); 
            unsigned int cur = timep;
            unsigned int num = 0;
            num = cur - old;
            cout << "num = " << num << endl;
            if(num > 30 && this->m_money  < ((num-30)/10+1)) 
            {

				this->m_deposit = this->m_deposit - ((num-30)/10+1);
				AccountData m;
				m.save(*this);
				return ((num-30)/10+1);
			}
            if(num > 30 && this->m_money  >= ((num-30)/10+1))
            {
				this-> m_money = this-> m_money -((num-30)/10+1);
				AccountData m;
				m.save(*this);
				return ((num-30)/10+1);
            }
            AccountData m;
            m.save(*this);
            return 0;      
        }
    }
    return -2;
}

Account* AccountData::load(unsigned int id)
{
	char path[128] = {};
	sprintf(path,"../data/account/%d.txt",id);
	FILE* frp = fopen(path,"r");
	if(frp == NULL)
	{
		return NULL;
	}
	Account* account = new Account;
	char buf[256] = {};
	fscanf(frp,"昵称:%s\n",buf);
	account->m_name = buf;
	bzero(buf,256);
	fscanf(frp,"ID:%u\n",&account->m_id);
	time_t tt;
	fscanf(frp,"注册时间:%ld\n", &tt );
	struct tm *t = localtime(&tt);
	account->m_regTime = *t;
	fscanf(frp,"信用值:%u\n",&account->m_credit);
	fscanf(frp,"密码:%s\n",buf);
	account->m_passwd = buf;
	bzero(buf,256);
	fscanf(frp,"余额:%u\n",&account->m_money);
	fscanf(frp,"押金:%u\n",&account->m_deposit);
	
	int num = 0;
	fscanf(frp,"预定记录数:%u\n",&num);
	for(int i=0;i<num;i++)
	{
		Account::Reserve reserve;
		time_t tt = 0;
		fscanf(frp,"预定时间:%ld ", &tt );
		struct tm *t = localtime(&tt);
		reserve.m_time = *t;
		fscanf(frp,"书号:%u ",&reserve.m_bookId);
		fscanf(frp,"账号:%u\n",&reserve.m_id);
		account->m_reserve.push_back(reserve);
	}

	int size = 0;
	fscanf(frp,"书单数:%d\n",&size);
	for(int i=0;i<size;i++)
	{
		int li = 0;
		fscanf(frp,"%u ",&li);
		account->m_vecList.push_back(li);
	}
	fscanf(frp,"\n");
	
	fscanf(frp,"收藏书单数:%d\n",&size);
	for(int i=0;i<size;i++)
	{
		int li = 0;
		fscanf(frp,"%u ",&li);
		account->m_collection.push_back(li);
	}
	fscanf(frp,"\n");

	unsigned int recsize = 0;
	fscanf(frp,"借书记录数:%u\n",&recsize);
	
	for(unsigned int i=0;i<recsize;i++)
	{
		Record record;
		fscanf(frp,"编号:%u ",&record.m_id);
		fscanf(frp,"书号:%u ",&record.m_bookId);
		char buf[256] = {};
		fscanf(frp,"书名:%s ",buf);
		record.m_bookName = buf;
		bzero(buf,256);
		fscanf(frp,"借书人ID:%u ",&record.m_perId);
		fscanf(frp,"借书人姓名:%s ",buf);
		record.m_name = buf;
		fscanf(frp,"租借天数:%u ",&record.m_day);
		time_t r_time = 0;
		fscanf(frp,"借书日期:%ld ",&r_time);
		struct tm *t = localtime(&r_time);
		record.m_broTime  = *t;
		int isReturn = 0;
		fscanf(frp,"是否归还:%d ",&isReturn);
		record.m_isReturn = isReturn?1:0;
		if(isReturn)
		{
			time_t r_time = 0;
			fscanf(frp,"归还日期:%ld ",&r_time);
			struct tm *t = localtime(&r_time);
			record.m_retTime  = *t;
		}
		fscanf(frp,"\n");
		account->m_record.push_back(record);
	}
	fclose(frp);
	return account;
}

void AccountData::save(Account& account)
{
	char path[128] = {};
	sprintf(path,"../data/account/%d.txt",account.m_id);
	FILE* fwp = fopen(path,"w");
	fprintf(fwp,"昵称:%s\n",account.m_name.c_str());
	fprintf(fwp,"ID:%u\n",account.m_id);
	fprintf(fwp,"注册时间:%ld\n", mktime(&account.m_regTime) );
	fprintf(fwp,"信用值:%u\n",account.m_credit);
	fprintf(fwp,"密码:%s\n",account.m_passwd.c_str());
	fprintf(fwp,"余额:%u\n",account.m_money);
	fprintf(fwp,"押金:%u\n",account.m_deposit);
	
	fprintf(fwp,"预定记录数:%u\n",account.m_reserve.size());
	for(unsigned int i=0;i<account.m_reserve.size();i++)
	{
		fprintf(fwp,"预定时间:%ld ", mktime(&account.m_reserve.at(i).m_time) );
		fprintf(fwp,"书号:%u ",account.m_reserve.at(i).m_bookId);
		fprintf(fwp,"账号:%u\n",account.m_reserve.at(i).m_id);
	}
	
	fprintf(fwp,"书单数:%d\n",account.m_vecList.size());
	for(unsigned int i=0;i<account.m_vecList.size();i++)
	{
		fprintf(fwp,"%u ",account.m_vecList.at(i));
	}
	fprintf(fwp,"\n");
	
	fprintf(fwp,"收藏书单数:%d\n",account.m_collection.size());
	for(unsigned int i=0;i<account.m_collection.size();i++)
	{
		fprintf(fwp,"%u ",account.m_collection.at(i));
	}
	fprintf(fwp,"\n");
	
	fprintf(fwp,"借书记录数:%u\n",account.m_record.size());
	for(unsigned int i=0;i<account.m_record.size();i++)
	{
		fprintf(fwp,"编号:%u ",account.m_record.at(i).m_id);
		fprintf(fwp,"书号:%u ",account.m_record.at(i).m_bookId);
		fprintf(fwp,"书名:%s ",account.m_record.at(i).m_bookName.c_str());
		fprintf(fwp,"借书人ID:%u ",account.m_record.at(i).m_perId);
		fprintf(fwp,"借书人姓名:%s ",account.m_record.at(i).m_name.c_str());
		fprintf(fwp,"租借天数:%u ",account.m_record.at(i).m_day);
		fprintf(fwp,"借书日期:%ld ",mktime(&account.m_record.at(i).m_broTime));
		fprintf(fwp,"是否归还:%d ",account.m_record.at(i).m_isReturn);
		if(account.m_record.at(i).m_isReturn)
		{
			fprintf(fwp,"归还日期:%ld ",mktime(&account.m_record.at(i).m_retTime));
		}
		fprintf(fwp,"\n");
	}
	fclose(fwp);
}
